package com.yupi.yupao.config;

import lombok.Data;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author eleanor
 * @Date 2024/11/9 15:42
 * @packageName:com.yupi.yupao.config
 * @ClassName:RedissonConfig
 *
 * redisson配置
 */
@Configuration
@ConfigurationProperties(prefix = "spring.redis")
@Data
public class RedissonConfig {

    private String host;

    private String port;

    private String password;

    @Bean
    public RedissonClient redissonClient(){
        // 1. 创建配置
        Config config=new Config();
        String redisAddress=String.format("redis://%s:%s",host,port);
        //  使用单个Redis，没有开集群 useClusterServers()  设置地址和使用库
        // 这里有坑，一定要加上密码！！！才能连接上redis
        config.useSingleServer().setAddress(redisAddress).setPassword(password).setDatabase(3);
        //2.创建实例
        RedissonClient redisson= Redisson.create(config);
        return redisson;
    }
}
